<template>
  <div class="address-management">
    <el-card class="main-card">
      <template #header>
        <div class="card-header">
          <span class="title">地址管理</span>
          <el-button type="primary" @click="handleAdd">添加地址</el-button>
        </div>
      </template>
      
      <el-table :data="addressList" style="width: 100%" v-loading="loading">
        <el-table-column prop="addressId" label="ID" width="80" />
        <el-table-column prop="userId" label="用户ID" width="100" />
        <el-table-column prop="name" label="收货人" width="120" />
        <el-table-column prop="phone" label="联系电话" width="150" />
        <el-table-column prop="area" label="所在地区" width="200" show-overflow-tooltip />
        <el-table-column prop="areaDetail" label="详细地址" show-overflow-tooltip />
        <el-table-column label="是否默认" width="100">
          <template #default="{ row }">
            <el-tag :type="row.isSelected === 1 ? 'success' : 'info'">
              {{ row.isSelected === 1 ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleView(row)">查看</el-button>
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加地址' : '编辑地址'"
      width="600px"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="收货人" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="所在地区" prop="area">
          <el-input v-model="form.area" />
        </el-form-item>
        <el-form-item label="详细地址" prop="areaDetail">
          <el-input v-model="form.areaDetail" type="textarea" />
        </el-form-item>
        <el-form-item label="是否默认">
          <el-switch v-model="form.isSelected" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      v-model="detailVisible"
      title="地址详情"
      width="600px"
      destroy-on-close
    >
      <div v-if="currentAddress" class="address-detail">
        <div class="detail-header">
          <h2>收货人：{{ currentAddress.name }}</h2>
          <el-tag :type="currentAddress.isSelected === 1 ? 'success' : 'info'">
            {{ currentAddress.isSelected === 1 ? '默认地址' : '非默认地址' }}
          </el-tag>
        </div>
        <div class="detail-content">
          <div class="detail-section">
            <el-descriptions :column="1" border>
              <el-descriptions-item label="地址ID">{{ currentAddress.addressId }}</el-descriptions-item>
              <el-descriptions-item label="用户ID">{{ currentAddress.userId }}</el-descriptions-item>
              <el-descriptions-item label="收货人">{{ currentAddress.name }}</el-descriptions-item>
              <el-descriptions-item label="联系电话">{{ currentAddress.phone }}</el-descriptions-item>
              <el-descriptions-item label="所在地区">{{ currentAddress.area }}</el-descriptions-item>
              <el-descriptions-item label="详细地址">{{ currentAddress.areaDetail }}</el-descriptions-item>
            </el-descriptions>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getAddressList, addAddress, updateAddress, deleteAddress, getAddressDetail } from "@/api/address";

const loading = ref(false);
const addressList = ref([]);
const dialogVisible = ref(false);
const detailVisible = ref(false);
const dialogType = ref('add');
const currentAddress = ref(null);
const formRef = ref(null);

const form = ref({
  addressId: '',
  name: '',
  phone: '',
  area: '',
  areaDetail: '',
  isSelected: 0
});

const rules = {
  name: [{ required: true, message: '请输入收货人姓名', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  area: [{ required: true, message: '请输入所在地区', trigger: 'blur' }],
  areaDetail: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
};

// 获取地址列表
const getList = async () => {
  loading.value = true;
  try {
    const res = await getAddressList();
    addressList.value = res.data;
  } catch (error) {
    console.log(error);
    ElMessage.error("获取地址列表失败");
  } finally {
    loading.value = false;
  }
};

// 添加地址
const handleAdd = () => {
  dialogType.value = 'add';
  form.value = {
    addressId: '',
    name: '',
    phone: '',
    area: '',
    areaDetail: '',
    isSelected: 0
  };
  dialogVisible.value = true;
};

// 编辑地址
const handleEdit = (row) => {
  dialogType.value = 'edit';
  form.value = { ...row };
  dialogVisible.value = true;
};

// 删除地址
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该地址吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteAddress(row.addressId);
      ElMessage.success('删除成功');
      getList();
    } catch (error) {
      console.log(error);
      ElMessage.error('删除失败');
    }
  });
};

// 查看详情
const handleView = async (row) => {
  try {
    const res = await getAddressDetail(row.addressId);
    currentAddress.value = res.data;
    detailVisible.value = true;
  } catch (error) {
    console.log(error);
    ElMessage.error('获取地址详情失败');
  }
};

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return;
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (dialogType.value === 'add') {
          await addAddress(form.value);
          ElMessage.success('添加成功');
        } else {
          await updateAddress(form.value);
          ElMessage.success('更新成功');
        }
        dialogVisible.value = false;
        getList();
      } catch (error) {
        console.log(error);
        ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败');
      }
    }
  });
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
.address-management {
  padding: 20px;
}

.main-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.address-detail {
  padding: 20px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
}

.detail-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.detail-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.detail-section {
  background: #f5f7fa;
  padding: 20px;
  border-radius: 8px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
  padding: 12px 0;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-button--link) {
  padding: 0 8px;
  font-size: 13px;
}

:deep(.el-descriptions__label) {
  width: 100px;
  font-weight: 500;
  color: #606266;
}

:deep(.el-descriptions__content) {
  color: #303133;
}

:deep(.el-dialog) {
  border-radius: 8px;
}

:deep(.el-dialog__header) {
  padding: 20px;
  margin: 0;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__footer) {
  padding: 20px;
  border-top: 1px solid #ebeef5;
}
</style> 